{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}
{{#service:functions}}{{#function:return_type}}
{{#function:starts_interaction?}}
{{ > common/fun_docblock}}
  {{type:name}} {{function:cpp_name}}();
{{/function:starts_interaction?}}{{^function:starts_interaction?}}
{{^function:sink?}}
{{^service:reduced_client?}}{{^function:creates_interaction?}}
{{ > common/fun_docblock}}
  virtual void {{function:cpp_name}}(std::unique_ptr<apache::thrift::RequestCallback> callback{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{ > common/fun_docblock}}
{{^service:reduced_client?}}{{^function:creates_interaction?}}
  virtual{{/function:creates_interaction?}}{{/service:reduced_client?}} void {{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions, std::unique_ptr<apache::thrift::RequestCallback> callback{{#function:creates_interaction?}}, const apache::thrift::InteractionHandle& handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:sink?}}
 protected:
  void fbthrift_serialize_and_send_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions, std::shared_ptr<apache::thrift::transport::THeader> header, apache::thrift::ContextStack* contextStack, {{ > types/callback_type}} callback{{#function:creates_interaction?}}, const apache::thrift::InteractionHandle& handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/function_param_list_client}}, bool stealRpcOptions = false);
 public:
{{^function:sink?}}

{{^function:sync_returns_by_outparam?}}
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}{{ > types/return_type_client}} sync_{{function:cpp_name}}({{ > service_common/function_param_list_client}});
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}{{ > types/return_type_client}} sync_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:sync_returns_by_outparam?}}
{{#function:sync_returns_by_outparam?}}
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}void sync_{{function:cpp_name}}({{function:cpp_return_type}}& _return{{function:comma}}{{ > service_common/function_param_list_client}});
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}void sync_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions, {{function:cpp_return_type}}& _return{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:sync_returns_by_outparam?}}

{{^service:reduced_client?}}{{^function:creates_interaction?}}{{^function:stream?}}
{{ > common/fun_docblock}}
  virtual folly::Future<{{ > types/return_type_client_lift_unit}}> future_{{function:cpp_name}}({{ > service_common/function_param_list_client}});
{{/function:stream?}}{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}folly::SemiFuture<{{ > types/return_type_client_lift_unit}}> semifuture_{{function:cpp_name}}({{ > service_common/function_param_list_client}});
{{^service:reduced_client?}}{{^function:creates_interaction?}}{{^function:stream?}}
{{ > common/fun_docblock}}
  virtual folly::Future<{{ > types/return_type_client_lift_unit}}> future_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:stream?}}{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{ > common/fun_docblock}}
  {{^service:reduced_client?}}{{^function:creates_interaction?}}virtual {{/function:creates_interaction?}}{{/service:reduced_client?}}folly::SemiFuture<{{ > types/return_type_client_lift_unit}}> semifuture_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{#function:creates_interaction?}}
{{ > common/fun_docblock}}
  std::pair<
    apache::thrift::Client<{{service:parent_service_qualified_name}}>::{{function:created_interaction}},
    folly::SemiFuture<{{ > types/return_type_client_no_handle_lift_unit}}>
  > eager_semifuture_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:creates_interaction?}}
{{^function:oneway?}}
{{^service:reduced_client?}}{{^function:creates_interaction?}}
{{#function:has_deprecated_header_client_methods}}
{{^function:stream?}}
{{ > common/fun_docblock}}
  virtual folly::Future<std::pair<{{ > types/return_type_client_lift_unit}}, std::unique_ptr<apache::thrift::transport::THeader>>> header_future_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:stream?}}
{{ > common/fun_docblock}}
  virtual folly::SemiFuture<std::pair<{{ > types/return_type_client_lift_unit}}, std::unique_ptr<apache::thrift::transport::THeader>>> header_semifuture_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:has_deprecated_header_client_methods}}
{{#type:sync_methods_return_try?}}
{{ > common/fun_docblock}}
  FOLLY_NODISCARD [[deprecated("To be replaced by new API soon")]] virtual folly::Try<apache::thrift::RpcResponseComplete<{{ > types/return_type_client}}>> sync_complete_{{function:cpp_name}}(
      apache::thrift::RpcOptions&& rpcOptions{{function:comma}} {{ > service_common/function_param_list_client}});
{{/type:sync_methods_return_try?}}
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:oneway?}}

#if FOLLY_HAS_COROUTINES
  {{!
  Coroutine functions are inline templates to prevent compile time and binary size regression
  }}
#if __clang__
{{ > common/fun_docblock}}
  template <int = 0>
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}({{ > service_common/function_param_list_client}}) {
    return co_{{function:cpp_name}}<false>(nullptr{{function:comma}}{{ > service_common/param_list}});
  }
{{ > common/fun_docblock}}
  template <int = 0>
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}}) {
    return co_{{function:cpp_name}}<true>(&rpcOptions{{function:comma}}{{ > service_common/param_list}});
  }
#else
  {{!
  GCC coroutines implementation is less stable and results in ICE compiling the code below.
  Use semifuture_ implementation instead.
  }}
{{ > common/fun_docblock}}
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}({{ > service_common/function_param_list_client}}) {
    {{^type:void?}}co_return {{/type:void?}}co_await folly::coro::detachOnCancel(semifuture_{{function:cpp_name}}({{ > service_common/param_list}}));
  }
{{ > common/fun_docblock}}
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}}) {
    {{^type:void?}}co_return {{/type:void?}}co_await folly::coro::detachOnCancel(semifuture_{{function:cpp_name}}(rpcOptions{{function:comma}}{{ > service_common/param_list}}));
  }
#endif
 private:
  template <bool hasRpcOptions>
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}(apache::thrift::RpcOptions* rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}}) {
    const folly::CancellationToken& cancelToken =
        co_await folly::coro::co_current_cancellation_token;
    const bool cancellable = cancelToken.canBeCancelled();
    apache::thrift::ClientReceiveState returnState;
    apache::thrift::ClientCoroCallback<{{#function:oneway?}}true{{/function:oneway?}}{{^function:oneway?}}false{{/function:oneway?}}> callback(&returnState, co_await folly::coro::co_current_executor);
    {{^function:oneway?}}
    auto protocolId = apache::thrift::GeneratedAsyncClient::getChannel()->getProtocolId();
    {{/function:oneway?}}
    auto [ctx, header] = {{function:cpp_name}}Ctx(rpcOptions);
    using CancellableCallback = apache::thrift::CancellableRequestClientCallback<{{#function:oneway?}}true{{/function:oneway?}}{{^function:oneway?}}false{{/function:oneway?}}>;
    auto cancellableCallback = cancellable ? CancellableCallback::create(&callback, channel_) : nullptr;
    static apache::thrift::RpcOptions* defaultRpcOptions = new apache::thrift::RpcOptions();
  {{^function:stream?}}
    auto wrappedCallback = apache::thrift::RequestClientCallback::Ptr(cancellableCallback ? (apache::thrift::RequestClientCallback*)cancellableCallback.get() : &callback);
  {{/function:stream?}}{{#function:stream?}}
    auto wrappedCallback = apache::thrift::createStreamClientCallback(
        apache::thrift::RequestClientCallback::Ptr(cancellableCallback ? (apache::thrift::RequestClientCallback*)cancellableCallback.get() : &callback),
      hasRpcOptions ? rpcOptions->getBufferOptions() : defaultRpcOptions->getBufferOptions());
  {{/function:stream?}}
  {{#function:creates_interaction?}}
    {{function:created_interaction}} handle(channel_, "{{function:created_interaction}}");
  {{/function:creates_interaction?}}
    const bool shouldProcessClientInterceptors = ctx && ctx->shouldProcessClientInterceptors();
    if (shouldProcessClientInterceptors) {
      co_await ctx->processClientInterceptorsOnRequest();
    }
    if constexpr (hasRpcOptions) {
      fbthrift_serialize_and_send_{{function:cpp_name}}(*rpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
    } else {
      fbthrift_serialize_and_send_{{function:cpp_name}}(*defaultRpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
    }
    if (cancellable) {
      folly::CancellationCallback cb(cancelToken, [&] { CancellableCallback::cancel(std::move(cancellableCallback)); });
      co_await callback.co_waitUntilDone();
    } else {
      co_await callback.co_waitUntilDone();
    }
    if (shouldProcessClientInterceptors) {
      co_await ctx->processClientInterceptorsOnResponse();
    }
    if (returnState.isException()) {
      co_yield folly::coro::co_error(std::move(returnState.exception()));
    }
  {{^function:oneway?}}
    returnState.resetProtocolId(protocolId);
    returnState.resetCtx(std::move(ctx));
    SCOPE_EXIT {
      if (hasRpcOptions && returnState.header()) {
        auto* rheader = returnState.header();
        if (!rheader->getHeaders().empty()) {
          rpcOptions->setReadHeaders(rheader->releaseHeaders());
        }
        rpcOptions->setRoutingData(rheader->releaseRoutingData());
      }
    };
    {{#type:void?}}
    if (auto ew = recv_wrapped_{{function:cpp_name}}(returnState)) {
      co_yield folly::coro::co_error(std::move(ew));
    }
    {{/type:void?}}
    {{^type:void?}}
    {{ > types/return_type_client_no_handle}} _return;
    if (auto ew = recv_wrapped_{{function:cpp_name}}(_return, returnState)) {
      co_yield folly::coro::co_error(std::move(ew));
    }
    {{/type:void?}}
  {{^function:creates_interaction?}}
    {{^type:void?}}
    co_return _return;
    {{/type:void?}}
  {{/function:creates_interaction?}}
  {{#function:creates_interaction?}}
    {{^type:void?}}
    co_return std::make_pair(std::move(handle), std::move(_return));
    {{/type:void?}}
    {{#type:void?}}
    co_return handle;
    {{/type:void?}}
  {{/function:creates_interaction?}}
  {{/function:oneway?}}
  }
 public:
#endif // FOLLY_HAS_COROUTINES

{{^service:reduced_client?}}{{^function:creates_interaction?}}
{{^function:stream?}}
{{ > common/fun_docblock}}
  virtual void {{function:cpp_name}}(folly::Function<void (::apache::thrift::ClientReceiveState&&)> callback{{#function:creates_interaction?}}, const apache::thrift::InteractionHandle& handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:stream?}}

{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:sink?}}{{#function:sink?}}
#if FOLLY_HAS_COROUTINES
{{ > common/fun_docblock}}
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}({{ > service_common/function_param_list_client}});
{{ > common/fun_docblock}}
  folly::coro::Task<{{ > types/return_type_client}}> co_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
#endif // FOLLY_HAS_COROUTINES
{{/function:sink?}}

{{#function:cpp_void?}}
{{^function:oneway?}}
{{ > common/fun_docblock}}
  static folly::exception_wrapper recv_wrapped_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{ > common/fun_docblock}}
  static void recv_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{^service:reduced_client?}}{{^function:creates_interaction?}}
  // Mock friendly virtual instance method
{{ > common/fun_docblock}}
  virtual void recv_instance_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{ > common/fun_docblock}}
  virtual folly::exception_wrapper recv_instance_wrapped_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:oneway?}}
{{/function:cpp_void?}}
{{^function:cpp_void?}}
{{ > common/fun_docblock}}
  static folly::exception_wrapper recv_wrapped_{{function:cpp_name}}({{ > types/return_type_client_no_handle}}& _return, ::apache::thrift::ClientReceiveState& state);
{{^function:sync_returns_by_outparam?}}
{{ > common/fun_docblock}}
  static {{ > types/return_type_client_no_handle}} recv_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{^service:reduced_client?}}{{^function:creates_interaction?}}
  // Mock friendly virtual instance method
{{ > common/fun_docblock}}
  virtual {{ > types/return_type_client_no_handle}} recv_instance_{{function:cpp_name}}(::apache::thrift::ClientReceiveState& state);
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:sync_returns_by_outparam?}}{{#function:sync_returns_by_outparam?}}
{{ > common/fun_docblock}}
  static void recv_{{function:cpp_name}}({{ > types/return_type_client_no_handle}}& _return, ::apache::thrift::ClientReceiveState& state);
{{^service:reduced_client?}}{{^function:creates_interaction?}}
  // Mock friendly virtual instance method
{{ > common/fun_docblock}}
  virtual void recv_instance_{{function:cpp_name}}({{ > types/return_type_client_no_handle}}& _return, ::apache::thrift::ClientReceiveState& state);
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:sync_returns_by_outparam?}}
{{^service:reduced_client?}}{{^function:creates_interaction?}}
{{ > common/fun_docblock}}
  virtual folly::exception_wrapper recv_instance_wrapped_{{function:cpp_name}}({{ > types/return_type_client_no_handle}}& _return, ::apache::thrift::ClientReceiveState& state);
{{/function:creates_interaction?}}{{/service:reduced_client?}}
{{/function:cpp_void?}}
 private:
  apache::thrift::SerializedRequest fbthrift_serialize_{{function:cpp_name}}(const RpcOptions& rpcOptions, apache::thrift::transport::THeader& header, apache::thrift::ContextStack* contextStack{{function:comma}}{{ > service_common/function_param_list_client}});
  template <typename RpcOptions>
  void fbthrift_send_{{function:cpp_name}}(apache::thrift::SerializedRequest&& request, RpcOptions&& rpcOptions, std::shared_ptr<apache::thrift::transport::THeader> header, {{ > types/callback_type}} callback{{#function:creates_interaction?}}, const apache::thrift::InteractionHandle& handle{{/function:creates_interaction?}});
  std::pair<::apache::thrift::ContextStack::UniquePtr, std::shared_ptr<::apache::thrift::transport::THeader>> {{function:cpp_name}}Ctx(apache::thrift::RpcOptions* rpcOptions);
{{^function:sink?}}
  template <typename CallbackType>
  folly::SemiFuture<{{ > types/return_type_client_lift_unit}}> fbthrift_semifuture_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}});
{{/function:sink?}}
 public:
{{/function:starts_interaction?}}{{/function:return_type}}{{/service:functions}}
